import user.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class    RegisterInerface {
    JFrame frame = new JFrame("注册");
    Font font1=new Font("宋体",Font.BOLD,15);
    Font font2=new Font("宋体",Font.BOLD,36);
    Font font3=new Font("宋体",Font.BOLD,20);
    public void init(int power) {
        //设置窗体关闭模式
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗体大小
        frame.setSize(400, 600);
        //设置窗体居中
        frame.setLocationRelativeTo(null);
        //取消布局
        frame.setLayout(null);
        //设置窗口不能改变大小
        frame.setResizable(false);
        //设置文章标题
        Box title1box=Box.createHorizontalBox();
        Box title2box=Box.createHorizontalBox();
        JLabel title1 = new JLabel(power+"级密钥输入正确,欢迎使用该系统");
        JLabel title2 = new JLabel("\n新管理注册");
        title1.setFont(font1);
        title2.setFont(font2);
        title1box.add(title1);
        title2box.add(title2);
        //定位标题
        title1box.setBounds(10,5,400,40);
        title2box.setBounds(100,70,400,40);
        frame.add(title1box);
        frame.add(title2box);
        //设置账户名
        Box user_namebox=Box.createHorizontalBox();
        JLabel nameLabel = new JLabel("用户名:");
        JTextField nameField = new JTextField(15);
        nameLabel.setFont(font3);
        nameField.setFont(font3);
        user_namebox.add(nameLabel);
        user_namebox.add(Box.createHorizontalStrut(20));
        user_namebox.add(nameField);
        //定位用户名
        user_namebox.setBounds(40,200,300,30);
        frame.add(user_namebox);
        //设置密码
        Box user_passwordbox= Box.createHorizontalBox();
        JLabel passwordLabel =new JLabel("密  码:");
        JPasswordField passwordField=new JPasswordField(15);
        passwordField.setEchoChar('*');
        passwordLabel.setFont(font3);
        passwordField.setFont(font3);
        user_passwordbox.add(passwordLabel);
        user_passwordbox.add(Box.createHorizontalStrut(20));
        user_passwordbox.add(passwordField);
        //定位密码
        user_passwordbox.setBounds(40,250,300,30);
        frame.add(user_passwordbox);
        //检验密码
        Box user_passwordbox_yn= Box.createHorizontalBox();
        JLabel passwordLabel_yn =new JLabel("检验密码:");
        JPasswordField passwordField_yn=new JPasswordField(15);
        passwordField_yn.setEchoChar('*');
        passwordLabel_yn.setFont(font3);
        passwordField_yn.setFont(font3);
        user_passwordbox_yn.add(passwordLabel_yn);
        user_passwordbox_yn.add(Box.createHorizontalStrut(20));
        user_passwordbox_yn.add(passwordField_yn);
        //定位密码
        user_passwordbox_yn.setBounds(18,300,322,30);
        frame.add(user_passwordbox_yn);
        //注册按钮
        Box btnbox=Box.createHorizontalBox();
        JButton btnYn=new JButton("注册");
        //返回登录按钮
        JButton backbtn=new JButton("返回");
        //挂载按钮
        btnbox.add(backbtn);
        btnbox.add(Box.createVerticalStrut(20));
        btnbox.add(btnYn);
        //定位按钮
        btnbox.setBounds(100,350,200,90);
        frame.add(btnbox);
        //为按钮注册点击时间
        //返回
        backbtn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new Blog_windows().init();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                frame.dispose();
            }
        });
        //注册
        btnYn.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username=nameField.getText().trim();
                String password=passwordField.getText().trim();
                String passwordYn=passwordField_yn.getText().trim();
                if(!password.equals(passwordYn)||User.user_queryYn(username)){
                    JOptionPane.showMessageDialog(frame,(User.user_queryYn(username)?"账户名已被占用\n":"")+(!password.equals(passwordYn)?"两次密码不同":""));
                }else{
                    if(User.ADD_user(username,password,power)){
                        JOptionPane.showMessageDialog(frame,"注册成功");
                        try {
                            new Blog_windows().init();
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                        frame.dispose();
                    }else{
                        JOptionPane.showMessageDialog(frame,"注册出现异常,请稍后尝试");
                    }
                }
            }
        });
        //设置窗体是否可见
        frame.setVisible(true);
    }
}
